% -----------------------
% Project: Secret and Overt Information Acquisition in Financial Markets
% This code produces Figure 2
%  J=1: monopoly: Panel a
%  J=2: duopoly: Panel b
% -----------------------
clear; clc;
Su = 1;%sigma_u
C = linspace(0.005,0.4,200);%info-acquisition cost
J=1;%the number of traders
%overt economy
Ho = NaN(size(C));%info acquisition
Lo = NaN(size(C));%liquidity
Mo = NaN(size(C));%market efficiency
%secret economy
Hs = NaN(size(C));
Ls = NaN(size(C));
Ms = NaN(size(C));

options = optimset('Display','off', 'TolFun', 1e-15,'TolX',1e-15); % Turn off Display
tolfval=1e-10;


for i = 1:length(C)
   c = C(i); 
   param = [c,Su];

   % Overt info acquisition
    fun = @(h) ( (h*(1+h)*(2+h)^2*(2+h+h*J)^4)/...
                 ((6*J^2-J-3)*h^2+(4*J^2+10*J-8)*h+8*J-4 )^2- Su^2/(4*J^3*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [h,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
    Ho(i) = h;
    Lo(i) = sqrt(h*(1+h)*J)/(Su*(2+(J+1)*h));
    Mo(i) = (2+h+h*J)/(2+h);
    
   % Secret info acquisition
    fun = @(h) ( h*(1+h)*(2+h+h*J)^2 - Su^2/(J*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [h,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
    Hs(i) = h;
    Ls(i) = sqrt(h*(1+h)*J)/(Su*(2+(J+1)*h));
    Ms(i) = (2+h+h*J)/(2+h);
       
end

figure(1);
plot(C/Su,Hs,'b-',C/Su,Ho,'r--','LineWidth',1.5); 
    legend('Secret','Overt','Interpreter','latex'); legend box off;
xlabel('$c/\sigma_u$','Interpreter','latex'); ylabel('$h^*$','Interpreter','latex');
   box off; 
set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');  
set(gcf, 'Position',  [100, -100, 650, 600]); 

figure(2);
plot(C/Su,1./Ls,'b-',C/Su,1./Lo,'r--','LineWidth',1.5); 
xlabel('$c/\sigma_u$','Interpreter','latex'); ylabel('$1/\lambda$','Interpreter','latex');
   box off; 
set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');
set(gcf, 'Position',  [100, -100, 650, 600]); 

figure(3);
plot(C/Su,Ms,'b-',C/Su,Mo,'r--','LineWidth',1.5); 
xlabel('$c/\sigma_u$','Interpreter','latex'); ylabel('$Var(\tilde{v}|\tilde{p})^{-1}$','Interpreter','latex');
   box off; 
set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');  
set(gcf, 'Position',  [100, -100, 650, 600]); 
